%------------------------------------------------------------------
%------------------------------------------------------------------
%   Author: Md. Sazzad Hussain (sazzad.hussain@sydney.edu.au)
%   Learning and Affect Technologies Engineering (LATTE)
%   University of Sydney, 2011
%------------------------------------------------------------------
%------------------------------------------------------------------

% %Siento Top Module (Main)
clc;
clear all;
%% Toolbox Set
% % Define the paths for library and codes

% rehash toolbox; 
% clear classes;

%-----Mac---------
codePath='/My_Drive/Drive1/USYD_Research/sazzadPhD_SVN/myFramework';
libPath='/My_Drive/Drive1/USYD_Research/sazzadPhD_SVN/myLib';

%-----Winhows-----
% codePath='D:\USYD_Research\sazzadPhD_SVN\myFramework';
% libPath='D:\USYD_Research\sazzadPhD_SVN\myLib';

myCodes = genpath(codePath);
myLib = genpath(libPath);
addpath(myCodes);%framework path
addpath(myLib);%library path

%define OS
opSys=1; % 1-Mac; 2-Windows
%% files I/O
% %location for input and output files
%dir for input/output files

if opSys==1
    %-----Mac---------
    indir='/My_Drive/Drive1/USYD_Research/Research Data/IAPS Journal/EmoCog_Norm/1_UserDep/2_FeatSelection/cfs/Arousal/MAT';
    outdir='/My_Drive/Drive1/USYD_Research/Research Data/IAPS Journal/EmoCog_Norm/1_UserDep/4_CVPara_knn';    
elseif opSys==2
    %-----Windows-----
%     indir='D:\USYD_Research\Research Data\IAPS Journal\EmoCog\2_Feature_Selection\SelfReport\2_Arousal_Self\MAT';
%     outdir='D:\USYD_Research\Research Data\IAPS Journal\EmoCog\3_Classification\cfs';
end
%% subject IDs
%subject ID from experiment

subjectID=[
% 'siento_iaps_sub1_2010-09-06'         %AT
% % 'siento_iaps_sub1_2010-09-07'     %bad GSR
% 'siento_iaps_sub1_2010-09-08'       %iaps only (rejected AT)
% % 'siento_iaps_sub1_2010-09-10'       %EMG bad, over aged)
% 'siento_iaps_sub1_2010-09-13'       %bad EMG
% 'siento_iaps_sub1_2010-09-14'         %AT
% 'siento_iaps_sub1_2010-09-15'         %AT
% 'siento_iaps_sub2_2010-09-15'         %iaps only 
% 'siento_iaps_sub1_2010-09-16'         %AT
% 'siento_iaps_sub2_2010-09-16'         %AT
% 'siento_iaps_sub1_2010-09-17'         %AT
% 'siento_iaps_sub1_2010-09-20'         %AT
% 'siento_iaps_sub2_2010-09-20'         %AT
% 'siento_iaps_sub1_2010-09-21'         %AT (not used for mapping)
% 'siento_iaps_sub1_2010-09-23'         %AT
% 'siento_iaps_sub2_2010-09-23'         %AT%%%resp crash after 104
% 'siento_iaps_sub1_2010-09-24'         %AT
% 'siento_iaps_sub1_2010-09-29'         %AT
% 'siento_iaps_sub1_2010-09-30'         %AT
% 'siento_iaps_sub2_2010-09-30'         %AT
% 'siento_iaps_sub1_2010-10-13'         %AT
% % 'arousal_allSub'
% % 'arousal_MV_emotion_allSub'
% % 'arousal_NV_emotion_allSub'
% % 'arousal_PV_emotion_allSub'
% % 'valence_allSub'
% % 'valence_HA_emotion_allSub'
% % 'valence_LA_emotion_allSub'
% % 'valence_MA_emotion_allSub'
% 'nicta1_sub1_2010-11-08'
% 'nicta1_sub1_2010-11-09'
% 'nicta1_sub1_2010-11-10'%eyefeat
% 'nicta1_sub1_2010-11-11'%eyefeat
% 'nicta1_sub1_2010-11-12'%eyefeat
% 'nicta1_sub1_2010-11-15'%eyefeat
% 'nicta1_sub1_2010-11-16'%eyefeat
% 'nicta1_sub2_2010-11-08'%eyefeat
% 'nicta1_sub2_2010-11-09'%eyefeat
% 'nicta1_sub2_2010-11-10'%eyefeat, vid started late
% 'nicta1_sub2_2010-11-11'%eyefeat
% 'nicta1_sub2_2010-11-12'%eyefeat
% 'nicta1_sub2_2010-11-15'%eyefeat, physio started late
% 'nicta1_sub2_2010-11-16'%eyefeat
% 'nicta1_sub3_2010-11-08'%eyefeat
% 'nicta1_sub3_2010-11-09'%eyefeat
% 'nicta1_sub3_2010-11-10'
% 'nicta1_sub3_2010-11-15'
% % 'nicta1_sub3_2010-11-16'%no video
% 'nicta1_sub4_2010-11-16'%eyefeat
% % 'cognitive_allSub'
% % 'cognitive_HV-HA_allFileFeat'
% % 'cognitive_HV-LA_allFileFeat'
% % 'cognitive_HV-MA_allFileFeat'
% % 'cognitive_LV-HA_allFileFeat'
% % 'cognitive_LV-LA_allFileFeat'
% % 'cognitive_LV-MA_allFileFeat'
% % 'cognitive_NE-TO_allFileFeat'
];
%% Biosignal Inspector
% % Shows physiological signals with face video for given annotations

% % a. load physio file, load video file, load annotation file
% % b. show all annotation instances
% % c. sync physio & video with annotation time chunk 
% workDir=pwd;
% fid=fopen([workDir '\siento_bioSigInsp\linkFile.txt'],'w');%open file
% fprintf(fid, '%s\n', 'SubjectID');
% for m=1:length(subjectID(:,1))
%     fprintf(fid, '%s\n', subjectID(m,:));%baseline duration
% end
% fclose(fid);%close file
% 
% %load GUI
% [workDir '\siento_bioSigInsp\' siento_biosigInspector_IAPS]
%% ECG Derived Respiration
% edr = edr(indir,outdir,subjectID,opSys);
% 
% if edr==1
%     disp('Respiration Derived');
% end
%% configure physio/channels
% % Define the window sizes of channels for feature extraction

% % window size, sampling rate, downsample size
% startTime=2.5; %start of window (sec), 0-file TS;
% winSizeECG=5;  %ECG
% winSizeGSR=5;  %GSR
% winSizeResp=5; %RESP
% winSizeEMG=5;  %EMG
% sRate=1000; %original sample rate
% dSample=5;  %downsample; 1-no ds
%% feature extraction from IAPS
% % Physiological feature extraction using AuBT (subjectID)

% dubug=0;%1-show physio raw in plot, 0-off
% featExt1=iapsFeatEx(subjectID,indir,startTime,winSizeECG,winSizeGSR,... 
% winSizeEMG,winSizeResp,sRate,dSample,outdir,dubug,opSys);
% if featExt1==1 
%     disp('Feat Ext COMPLETE (IAPS)');
% end

% %note: check aubtProxy
%% feature extraction from AutoTutor
% % Physiological feature extraction using AuBT (subjectID)
% dubug=0;%1-show physio raw in plot, 0-off
% featExt2=autoTutorFeatEx(subjectID,indir,winSizeECG,winSizeGSR,... 
% winSizeEMG,winSizeResp,sRate,dSample,outdir,opSys,dubug);
% if featExt2==1 
%     disp('Feat Ext COMPLETE (AutoTutor)');
% end
% %note: check aubtProxy
%% feature extraction from IAPS-congnitiveLoad

% dubug=0;%1-show physio raw in plot, 0-off
% featExt3=EmoCogFeatEx(subjectID,indir,startTime,winSizeECG,winSizeGSR,...
% winSizeResp,sRate,dSample,outdir,dubug,opSys);
% if featExt3==1 
%     disp('Feat Ext COMPLETE (EmoCog)');
% end
%note: check aubtProxy

%% Feature Merge
% %Combine features from different files, channels, window sizes (subjectID)

% nClasses=1;%total number of class col in file
% 
% merge1=mergeFeat(indir, outdir, subjectID, nClasses,opSys);
% 
% if merge1==1 
%     disp('Feat Merge COMPLETE');
% end
%% Instance Merge
% %Merge all features from all instances (dump CSV)

% merge2= mergeInst(indir, outdir, opSys)
% 
%  if merge2==1
%      disp('Instance Merge COMPLETE');
%  end
%% Selective Class
% %Select instances based on selected class (dump CSV files)

% colNum=2; %col used from RIGHT
% className='MediumValence';
% filename='arousal_MV';
% selClass= selectiveClass(indir, outdir, colNum, className, filename, opSys)
% 
%  if selClass==1
%      disp('Selective Class COMPLETE');
%  end
%% Normalize Features
% %Normalize all features (dump CSV)

% nClasses=8;%total number of class col in file
% 
% norm1=normFeat(indir, outdir, nClasses, opSys);
% 
% if norm1==1 
%     disp('Feat Normalize COMPLETE');
% end
%% CSV to Mat convertor
% %dump CSV weka format files in indir and find converted MAT files in outdir

% numClasses=1;%total number of class col in file
% setClass=1;%col number (from left) for selected class 
% conv=csv2mat(indir, outdir, numClasses, setClass,opSys)
% 
% if conv==1 
%     disp('CSV to MAT conversion complete');
% end
%% Feature Selection
% %Chi-Square & Correlation Feature Selection (CFS) Tehcniques
% %dump MAT format files in indir for siento_featsel()

% featWinRange=[
% % '1:67_rsp'
% % '68:151_ecg'
% % '152:218_edr'
% % '1:151_ecg-rsp'
% % '68:218_ecg-edr'
% ];
% 
% % featRange=[
% % %     '1:224'     %Color
% % %     '225:460'   %Move
% % %     '461:796'   %ECG
% % %     '797:880'   %SC
% % %     '881:1148'  %Resp
% % %     '461:1148'  %Physio
% % %     '1:460'     %Video
% % %     '1:1148'    %Fusion  
% % ];
% 
% % featWinRange=[ %feat range_modality
% % % '1:56_color-full'
% % % '57:112_color-first'
% % % '113:168_color-mid'
% % % '169:224_color-last'
% % % '225:283_move-full'
% % % '284:342_move-first'
% % % '343:401_move-mid'
% % % '402:460_move-last'
% % % '461:544_ecg-full'
% % % '545:628_ecg-first'
% % % '629:712_ecg-mid'
% % % '713:796_ecg-last'
% % % '797:817_sc-full'
% % % '818:838_sc-first'
% % % '839:859_sc-mid'
% % % '860:880_sc-last'
% % % '881:947_rsp-full'
% % % '948:1014_rsp-first'
% % % '1015:1081_rsp-mid'
% % % '1082:1148_rsp-last'
% % ];
% 
% featRange=regexp(featWinRange,'_','split');%split winRange & modalityName
% 
% fileName=char(strcat('cfs_cognitive_', featRange(2)));
% 
% selFS=2; %1-Chi-Sq; 2-CFS)
% rank=0;
% sel = siento_featsel(indir,outdir,char(featRange(1)),selFS,rank,libPath,codePath,opSys,fileName);
% 
% if sel==1
%     disp('Feature Selection complete');
% end
%% Feature Analysis
% %Analysis on feature selection 
% %Link a specific .txt file contaning feature list

% fileName='cognitive_cfs';%file name for storing info
% 
% ch={ %must match feature head
% 'color'
% 'move'
% 'ecg'
% 'sc'
% % 'emg'
% 'rsp'
% };
% 
% win={ %must match feature tail with (e.g. '_win'), comment array for unique feat list
% % '_0-10'
% % '_0-5'
% % '_5-5'
% % '_2.5-5'
% %
% '_0-12'
% '_0-6'
% '_6-6'
% '_3-6'
% };
% 
% fAn=featAnalysis(indir,outdir,ch,win,fileName,opSys);
% if fAn==1
%     disp('Feature Analysis complete');
% end
%% Classfication
% % %indir: feat subfolders, subfolder: feat files 
% % dump MAT format files in indir/subfolder for wekaClassify()
% % based on MatlabArsenal library

% fileHeader='cfs_arousal';
% 
% % set to same random seeds
% s = RandStream('mt19937ar','seed',0);
% RandStream.setDefaultStream(s);
% 
% dataShuffle='classify -sf 1'; %shuffle data (0-no; 1-yes)
% trainTest=' -- cross_validate -t 10'; %training-testing (crossval -t #fold)
% baseClassfier=' -- WekaClassify -MultiClassWrapper 0'; %classfer type/wrapper
% cost=0;%[1-enable costsensitive classifier]
% 
% % % %------------------------------------SVM---------------------------------
% % %name of resutls file
% % fileName=[fileHeader '_svm'];
% % %SVM
% % wekaClassfier=' -- functions.SMO -C 1.0 -E 1.0 -G 0.01 -A 1000003 -T 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1';
% % %construct MatlabArsenal classfier string
% % classifier_string=[dataShuffle trainTest baseClassfier wekaClassfier];
% % %weka classification
% % cl=siento_wekaClassify(indir, outdir, classifier_string,cost,fileName,opSys,codePath);
% % 
% % % %-----------------------------------KNN1---------------------------------
% % %name of resutls file
% % fileName=[fileHeader '_knn1'];
% % %KNN1
% % wekaClassfier=' -- lazy.IB1';
% % %construct MatlabArsenal classfier string
% % classifier_string=[dataShuffle trainTest baseClassfier wekaClassfier];
% % %weka classification
% % cl=siento_wekaClassify(indir, outdir, classifier_string,cost,fileName,opSys,codePath);
% % 
% % % %------------------------------------KNN3--------------------------------
% % %name of resutls file
% % fileName=[fileHeader '_knn3'];
% % %KNN3
% % wekaClassfier=' -- lazy.IBk -K 3 -W 0';
% % %construct MatlabArsenal classfier string
% % classifier_string=[dataShuffle trainTest baseClassfier wekaClassfier];
% % %weka classification
% % cl=siento_wekaClassify(indir, outdir, classifier_string,cost,fileName,opSys,codePath);
% % 
% % % %------------------------------------dTree-------------------------------
% % %name of resutls file
% % fileName=[fileHeader '_dtree'];
% % %Decision Tree
% % wekaClassfier=' -- trees.J48 -C 0.25 -M 2';
% % %construct MatlabArsenal classfier string
% % classifier_string=[dataShuffle trainTest baseClassfier wekaClassfier];
% % %weka classification
% % cl=siento_wekaClassify(indir, outdir, classifier_string,cost,fileName,opSys,codePath);
% 
% % % %------------------------------------VOTE--------------------------------
% % %name of resutls file
% % fileName=[fileHeader '_vote'];
% % %Vote Classifier (AVG): SVM1, KNN1,KNN3, J48
% % wekaClassfier=' -- meta.Vote -B "weka.classifiers.functions.SMO -C 1.0 -E 1.0 -G 0.01 -A 1000003 -T 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1" -B "weka.classifiers.lazy.IB1 " -B "weka.classifiers.lazy.IBk -K 3 -W 0" -B "weka.classifiers.trees.J48 -C 0.25 -M 2"'; %classfier string from weka
% % %construct MatlabArsenal classfier string
% % classifier_string=[dataShuffle trainTest baseClassfier wekaClassfier];
% % %weka classification
% % cl=siento_wekaClassify(indir, outdir, classifier_string,cost,fileName,opSys,codePath);
% 
% % % %-----------------------------------COSTS------------------------------
% % %name of resutls file
% % fileName=[fileHeader '_cost'];
% % % %Cost Sensitive Classifier, Vote Classifier (AVG): SVM1, KNN1,KNN3, J48
% % wekaClassfier=' -- meta.CostSensitiveClassifier -S 1 -W weka.classifiers.meta.Vote -- -B "weka.classifiers.functions.SMO -C 1.0 -E 1.0 -G 0.01 -A 1000003 -T 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1" -B "weka.classifiers.lazy.IB1 " -B "weka.classifiers.lazy.IBk -K 3 -W 0" -B "weka.classifiers.trees.J48 -C 0.25 -M 2"'; %classfier string from weka
% % cost=1;
% % %construct MatlabArsenal classfier string
% % classifier_string=[dataShuffle trainTest baseClassfier wekaClassfier];
% % %weka classification
% % cl=siento_wekaClassify(indir, outdir, classifier_string,cost,fileName,opSys,codePath);
% 
% % % %------------------------------------Test--------------------------------
% % %name of resutls file
% % fileName=[fileHeader '_m5'];
% % wekaClassfier=' -- rules.M5Rules -M 4.0'; %classfier string from weka
% % % wekaClassfier=' -- meta.RandomCommittee -S 1 -I 10 -W weka.classifiers.trees.RandomTree -- -K 0 -M 1.0 -S 1'; %classfier string from weka
% % %construct MatlabArsenal classfier string
% % classifier_string=[dataShuffle trainTest baseClassfier wekaClassfier];
% % %weka classification
% % cl=siento_wekaClassify(indir, outdir, classifier_string,cost,fileName,opSys,codePath);
% 
% % % % %------------------------------------(CVPara)--------------------------------
% % %name of resutls file
% % fileName=[fileHeader '_CVPara_knn'];
% % 
% % %CVParameterSelection
% % wekaClassfier=' -- meta.CVParameterSelection -P "E 1.0 3.0 1.0" -P "C 0.01 100.0 2.0" -X 10 -S 1 -W weka.classifiers.functions.SMO -- -C 1.0 -E 1.0 -G 0.01 -A 1000003 -T 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1';
% % % wekaClassfier=' -- meta.CVParameterSelection -X 10 -S 1 -W weka.classifiers.meta.Vote -- -B "weka.classifiers.functions.SMO -C 1.0 -E 1.0 -G 0.01 -A 1000003 -T 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1" -B "weka.classifiers.lazy.IBk -K 1 -W 0" -B "weka.classifiers.trees.J48 -C 0.25 -M 2'; %classfier string from weka
% % %construct MatlabArsenal classfier string
% % classifier_string=[dataShuffle trainTest baseClassfier wekaClassfier];
% % %weka classification
% % cl=siento_wekaClassify(indir, outdir,classifier_string,cost,fileName,opSys,codePath);
% 
% if cl==1
%     disp('Classification complete');
% end
%% Decision Fusion Classificaion
% % %indir: feat/decision subfolders, subfolder: classfier decision files 
% % dump MAT format files in indir/subfolder
% % based on weighed majority voting

% fileName='decFusion_mVote';
% nFoldCV=10; %number of cross validations
% clD=siento_mvoteClassify(indir, outdir, fileName, nFoldCV, opSys);
% if clD==1
%     disp('Decision Fusion Classification complete');
% end
%% Classification Resutls
%dump all accuracy files to indir/acc & indir/prf.
%calcualtes mean/std of classification accuracy given subjects

% numSub=20;
% numClass=3;
% fName='results_csf';
% res1=siento_avgAcc(opSys,indir,outdir,numSub,fName);
% res2=siento_avgPRF(opSys,indir,outdir,numSub,numClass,fName);
% 
% if (res1==1 & res2==1)
%     disp('Result Generated');
% end

%% Statistical Test
% numClasses=6;%total number of class col in file
% classA=5;%ref class
% classB=2;%test class 
% stat = testStati(indir,outdir,opSys,numClasses,classA,classB)
% 
% if stat==1 
%     disp('Correlation test complete');
% end

%% run self report stat program (IAPS)
% mode=1; %mode-1 (IAPS), mode-2 (AutoTutor)
% statExt1=stat_selfReport(mode,subjectID,indir,outdir,opSys);
% if statExt1==1 
%     disp('StatIAPS COMPLETE');
% end
% %note: use original self report format
%% run self report stat program (IAPS-Cognitive load)
% statExt2=stat_emoCog(subjectID,indir,outdir,opSys);
% if statExt2==1 
%     disp('Stat IAPS-Cognitive COMPLETE');
% end

%% Toolbox clear
rmpath(myCodes);
rmpath(myLib);